package com.wzqj.agent.app.device.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.app.device.dao.AppDeviceDao;
import com.wzqj.agent.app.device.domain.Device;
import com.wzqj.agent.app.device.service.ApiDeviceService;
import com.wzqj.agent.common.ConfigHelper;
import com.wzqj.agent.common.Log;
import com.wzqj.agent.common.SoaConfig;
import com.wzqj.wsp.response.ResponseValue;
import com.wzqj.wsp.response.ResultStatus;
import com.wzqj.wsp.soa.SOAUtil;

@Service("apiDeviceService")
public class ApiDeviceServiceImpl implements ApiDeviceService {

	
	/**
     * 运营支撑平台服务
     */
    private static final String ACC_PROVIDER_APP_CODE = ConfigHelper.getProperty("ACC_PROVIDER_APP_CODE");
	/**
	 * 根据en查询设备
	 */

    private static final String ACC_DEVICE_EN = "devices/listByEns";
    /**
	 * 绑定设备到店铺
	 */
	private static final String ACC_BIND_DEVICE_STORE = "merchant/bindDeviceToMerchant";
	
	
	@Autowired
	private AppDeviceDao appDeviceDao;
	/**
     * 绑定设备到店铺
     * @param mcode
     * @param en
     * @return
     */
	@Override
	public JSONObject bindDeviceToStore(String mcode, String en) {

        /**************************备用：查询设备信息接口***********************************
        JSONObject ens = new JSONObject();
        ens.put("ens", ens);
        ResponseValue isExit = SOAUtil.getResValFromSOA(ACC_PROVIDER_APP_CODE, ACC_DEVICE_EN,ens,soaParam);
        System.out.println("isExit:"+isExit);
        if(isExit == null){
	        if(isExit.getStatus() == ResultStatus.NO_VALUE_STATUS){
	            return null;
	        }
        }
        ***********************************************************
        **/
		JSONObject soaParam = SoaConfig.getSoaParam();
        JSONObject params = new JSONObject();
        params.put("en", en);
        params.put("mcode", mcode);
        ResponseValue res = SOAUtil.getResValFromSOA(ACC_PROVIDER_APP_CODE, ACC_BIND_DEVICE_STORE, params,soaParam);
        System.out.println("--bindDeviceToStore:"+res.toString());
        if(res != null && res.getStatus() == ResultStatus.SUCCESS_STATUS){
        	Device device = new Device();
        	device.setMcode(mcode);
        	device.setEn(en);
        	appDeviceDao.insert(device);
        	JSONObject data = new JSONObject();
        	data.put("mcode", res.getData());
        	System.out.println("data:"+data);
            return data;
        }
        return null;
	}
	
	
	
	
}
